package com.xian.controller;

import com.xian.pojo.Permission;
import com.xian.pojo.Role;
import com.xian.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    // 查询所有角色
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        List<Role> roles = roleService.findAll();
        System.out.println(roles);
        mv.addObject("roleList",roles);
        mv.setViewName("role-list");
        return mv;
    }

    //添加一个角色
    @RequestMapping("save.do")
    public String save(Role role){
        roleService.save(role);
        return "redirect:findAll.do";
    }
    // 根据ID查询角色
    @RequestMapping("findById")
    public ModelAndView findById(String id){
        ModelAndView mv = new ModelAndView();
        Role role = roleService.findById(id);
        mv.addObject("role",role);
        mv.setViewName("role-show");
        return mv;
    }
    // 查询角色没有的权限
    @RequestMapping("/findUserByIdAndAllRole.do")
    public ModelAndView addPermission(@RequestParam("id") String id){
        ModelAndView mv = new ModelAndView();
        List<Permission> permissions = roleService.findNoP(id);
        mv.addObject("permissions",permissions);
        mv.addObject("roleId",id);
        mv.setViewName("role-permission-add");
        return mv;
    }
    // 给指定角色添加权限
    @RequestMapping("/addRoleToUser.do")
    public String addRoleToUser(@RequestParam("roleId") String id,@RequestParam(name = "ids",required = false) String[] pIds){
        roleService.addPermission(id,pIds);
        return "redirect:findAll.do";
    }
}
